
ARCH=@ARCH@
CC=@CC@
CXX=@CXX@

ifeq (@CROSS_COMPILING@,yes)
  DEPENDS_DIR = $(shell grep "^PREFIX=/" @abs_top_srcdir@/tools/depends/Makefile.include | sed 's/PREFIX=//g')
  DVDNAV_A = $(DEPENDS_DIR)/lib/libdvdnav.a
  DVDREAD_A = $(DEPENDS_DIR)/lib/libdvdread.a
  DVDCSS_A = $(DEPENDS_DIR)/lib/libdvdcss.a
else
  DVDNAV_A = @abs_top_srcdir@/tools/depends/target/libdvdnav/dvdnav-install/lib/libdvdnav.a
  DVDREAD_A = @abs_top_srcdir@/tools/depends/target/libdvdread/dvdread-install/lib/libdvdread.a
  DVDCSS_A = @abs_top_srcdir@/tools/depends/target/libdvdcss/dvdcss-install/lib/libdvdcss.a
endif

SYSDIR = @abs_top_srcdir@/system/players/VideoPlayer
SOS = libdvdnav-$(ARCH).so

WRAPPER = @abs_top_srcdir@/xbmc/cores/DllLoader/exports/wrapper.o
WRAPPER_DEF = @abs_top_srcdir@/xbmc/cores/DllLoader/exports/wrapper.def
WRAPPER_MACH_ALIAS = @abs_top_srcdir@/xbmc/cores/DllLoader/exports/wrapper_mach_alias

ifeq ($(findstring osx,$(ARCH)),osx)
  SO_LDFLAGS = $(LDFLAGS) -bundle -undefined dynamic_lookup -read_only_relocs suppress
else
  SO_LDFLAGS = $(LDFLAGS) -shared -fPIC -rdynamic
endif

ifeq ($(ARCH), powerpc-osx)
  # Add -lbundle1.o for powerpc-osx
  BUNDLE1_O = -lbundle1.o
endif

ifeq ($(ARCH),arm-osx)
  BUILD_DVDCSS = 0
else
  BUILD_DVDCSS = 1
endif
ifeq ($(BUILD_DVDCSS),1)
  SOS += libdvdcss-$(ARCH).so
  DVDREAD_DEPS = $(DVDCSS_A)
  DVDCSS_OBJS = libdvdcss/*.o
endif


SLIB = $(addprefix $(SYSDIR)/, $(SOS))

DISTCLEAN_FILES=libdvdcss/* libdvdnav/* libdvdread/*

all: $(SLIB)

ifeq ($(findstring osx,$(ARCH)), osx)

$(SYSDIR)/libdvdcss-$(ARCH).so:  $(WRAPPER) $(DVDREAD_DEPS)
	[ -d libdvdcss ] || mkdir libdvdcss
	cd libdvdcss; ar x $(DVDCSS_A)
	$(CC) $(SO_LDFLAGS) -Wl,-alias_list,$(WRAPPER_MACH_ALIAS) -o $@ \
                $(WRAPPER) $(DVDCSS_OBJS) $(BUNDLE1_O)

$(SYSDIR)/libdvdnav-$(ARCH).so: $(WRAPPER) $(DVDNAV_A) $(DVDREAD_A) $(DVDREAD_DEPS)
	[ -d libdvdread ] || mkdir libdvdread
	[ -d libdvdnav ] || mkdir libdvdnav
	[ $(BUILD_DVDCSS) -eq 1 ] && { [ -d libdvdcss ] || mkdir libdvdcss; } || :
	[ $(BUILD_DVDCSS) -eq 1 ] && { cd libdvdcss && ar x $(DVDCSS_A); } || :
	cd libdvdnav; ar x $(DVDNAV_A)
	cd libdvdread; ar x $(DVDREAD_A)
	$(CC) $(SO_LDFLAGS) -Wl,-alias_list,$(WRAPPER_MACH_ALIAS) -o $@ \
                $(WRAPPER) libdvdread/*.o libdvdnav/*.o $(DVDCSS_OBJS) $(BUNDLE1_O)

else
$(SYSDIR)/libdvdcss-$(ARCH).so: $(WRAPPER) $(WRAPPER_DEF) $(DVDCSS_A)
	[ -d libdvdcss ] || mkdir libdvdcss
	cd libdvdcss; ar x $(DVDCSS_A)
	$(CC) -o $@ $(SO_LDFLAGS) -Wl,--soname,$@ $(DVDCSS_OBJS) -Wl,--unresolved-symbols=ignore-all -lm \
        `cat $(WRAPPER_DEF)` $(WRAPPER)

$(SYSDIR)/libdvdnav-$(ARCH).so: $(WRAPPER) $(WRAPPER_DEF) $(DVDNAV_A) $(DVDREAD_A) $(DVDCSS_A)
	[ -d libdvdread ] || mkdir libdvdread
	[ -d libdvdnav ] || mkdir libdvdnav
	[ $(BUILD_DVDCSS) -eq 1 ] && { [ -d libdvdcss ] || mkdir libdvdcss; } || :
	[ $(BUILD_DVDCSS) -eq 1 ] && { cd libdvdcss && ar x $(DVDCSS_A); } || :
	cd libdvdnav; ar x $(DVDNAV_A)
	cd libdvdread; ar x $(DVDREAD_A)
	$(CC) -o $@ $(SO_LDFLAGS) -Wl,--soname,$@ libdvdread/*.o libdvdnav/*.o $(DVDCSS_OBJS) -lm -Wl,--unresolved-symbols=ignore-all \
                `cat  $(WRAPPER_DEF)` $(WRAPPER)
endif

$(WRAPPER_DEF):
	$(MAKE) -C @abs_top_srcdir@/xbmc/cores/DllLoader/exports wrapper.def

$(DVDNAV_A): $(DVDREAD_A)
	$(MAKE) -C @abs_top_srcdir@/tools/depends/target/libdvdnav

$(DVDREAD_A): $(DVDREAD_DEPS)
	$(MAKE) -C @abs_top_srcdir@/tools/depends/target/libdvdread

ifeq ($(BUILD_DVDCSS),1)
$(DVDCSS_A):
	$(MAKE) -C @abs_top_srcdir@/tools/depends/target/libdvdcss
endif

include @abs_top_srcdir@/Makefile.include

